package com.microblogforsim.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.microblogforsim.R;
import com.microblogforsim.activity.ChatActivity;
import com.microblogforsim.adapter.Message_List_Adapter;
import com.microblogforsim.model.Message;
import com.microblogforsim.ui.MessageDialog;

import java.util.ArrayList;

public class Fragment_Message extends Fragment {
    private View view;
    private ListView message_list;
    private Message_List_Adapter adapter;
    private ArrayList<Message> data;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_message,container,false);
        init();
        return view;
    }

    private void init() {
        message_list = view.findViewById(R.id.message_list);
        adapter = new Message_List_Adapter(getContext());
        data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            data.add(new Message());
        }
        adapter.replayAll(data);
        message_list.setAdapter(adapter);

        message_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getContext(), ChatActivity.class));
            }
        });
        message_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showDialog();
                return true;
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            更新数据
        }
    }

    private void showDialog(){
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message,null);
        final Dialog dialog = new Dialog(getContext(),R.style.mDialog);
        dialog.setContentView(dialogView);
        dialog.show();
        TextView btn_toTop = dialogView.findViewById(R.id.dialog_message_btn_toTop);
        TextView btn_delete = dialogView.findViewById(R.id.dialog_message_btn_delete);
        btn_toTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
